package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.response.ContractDetailResponse;
import com.yanchangapp.ui.view.ContractDetailAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-19.
 * description: 合同详情的P层
 */
public class ContractDetailAtPresenter extends BasePresenter<ContractDetailAtView> {

    public ContractDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryContractDetail(String id){
        HeadApiRetrofit.getInstance().getContractDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ContractDetailResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ContractDetailResponse> contractDetailResponseBaseResponse) {
                        if (getView() != null){
                            if (AppConstant.REQUEST_SUCCESS == contractDetailResponseBaseResponse.getCode()){
                                getView().getContractDetailSuccess(contractDetailResponseBaseResponse.getData());
                            }else {
                                getView().getContractDetailFailure(contractDetailResponseBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().getContractDetailFailure(exception.getErrorInfo());
                        }
                    }
                });

    }
}
